<template>
        <el-container>
            <el-header>管理页面</el-header>
            <el-main>
                <el-row>
                    <el-col :span="24">
                        配置信息
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form>
                            <el-form-item label="是否需要审核">
                                <el-select v-model="systemConfig.isNeedAudit">
                                    <el-option value="1" label="是" />
                                    <el-option value="2" label="否" />
                                </el-select>
                            </el-form-item>
                            <el-form-item>
                                <el-button @click="saveConfig">保存</el-button>
                            </el-form-item>
                        </el-form>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-table
                                v-loading="listLoading"
                                :data="tableData"
                                style="width: 100%">
                            <el-table-column
                                    prop="name"
                                    label="厦门996或单休的公司全名"
                                    width="300">
                            </el-table-column>
                            <el-table-column
                                    prop="reason"
                                    label="理由"
                                    width="480">
                            </el-table-column>
                            <el-table-column
                                    prop="others"
                                    label="其他信息">
                            </el-table-column>
                            <el-table-column label="操作">
                                <template slot-scope="scope">
                                    <el-button  @click="del(scope.$index)">删除</el-button>
                                    <el-button v-if="scope.row.status==='WAIT_AUDIT'" @click="pass(scope.$index)">审核通过</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </el-col>
                </el-row>

                <div style="margin-top: 30px">
                    <el-pagination v-show="queryParam.total>0" :current-page="queryParam.current"
                                   :page-sizes="[10,20,30, 50]" :page-size="queryParam.size" :total="queryParam.total"
                                   background layout="total, sizes, prev, pager, next, jumper"
                                   @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
                </div>
            </el-main>
            <el-footer></el-footer>
        </el-container>
</template>

<script>
    import companyApi from './api/companyApi'
    import configApi from './api/configApi'
    import userApi from './api/userApi'

    export default {
        name: 'app',
        data: function () {
            return {
                listLoading: false,
                systemConfig: {
                    isNeedAudit: ''
                },
                queryParam: {
                    current: 1,
                    size: 10,
                    total: 0,
                    name: ''
                },
                tableData: []
            }
        },
        mounted() {
            this.checkLogin()
            this.search()
            this.getConfig()
        },
        methods: {
            toAdd() {
                this.$router.push({path: "/add"})
            },
            checkLogin() {
                userApi.checkLogin().then(response => {
                    if (response.data.code !== 200) {
                        this.$router.push({"path": "/login"})
                    }
                })
            },
            getConfig(){
                configApi.getConfig({key: "needAudit"}).then(response=>{
                    if(response.data.code === 200){
                        this.systemConfig.isNeedAudit = response.data.data
                    }
                })
            },
            // 处理每页大小改变
            handleSizeChange(val) {
                this.queryParam.size = val
                this.search()
            },
            // 处理页数改变
            handleCurrentChange(val) {
                this.queryParam.current = val
                this.search()
            },
            // 列表
            search() {

                this.listLoading = true
                companyApi.managePage(this.queryParam).then(response => {
                    if (response.data.code === 200) {
                        var responseData = response.data.data
                        this.tableData = responseData.records
                        this.queryParam.current = responseData.current
                        this.queryParam.total = responseData.total
                    } else {
                        this.$message.error(response.data.msg)
                    }
                    this.listLoading = false
                })
            },
            saveConfig() {
                configApi.setConfig({key:"needAudit", value: this.systemConfig.isNeedAudit}).then(response=>{
                    if(response.data.code === 200){
                        this.$alert("保存成功")
                    }
                })
            },
            del(i){
                var postData = {}
                postData.id = this.tableData[i].id
                companyApi.auditNotValid(postData).then(response=>{
                    if(response.data.code === 200){
                        this.$alert('保存成功', '提示', {
                            confirmButtonText: '确定',
                            callback: () => {
                                this.search()
                            }
                        })
                    }
                })
            },
            pass(i){
                var postData = {}
                postData.id = this.tableData[i].id
                companyApi.auditPass(postData).then(response=>{
                    if(response.data.code === 200){
                        this.$alert('保存成功', '提示', {
                            confirmButtonText: '确定',
                            callback: () => {
                                this.search()
                            }
                        })
                    }
                })
            }
        }
    }
</script>

<style>

</style>
